package com.shitu.soulemo.rn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.facebook.react.bridge.ReadableMap;
import com.shitu.base.rn.BaseRNActivity;
import com.shitu.soulemo.utils.AccountHelper;

public class RNIdSwitchActivity extends BaseRNActivity {
    public static void start(Activity activity) {
        Intent intent = new Intent(activity, RNIdSwitchActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBundle("base", RNHelper.getBaseBundle(AccountHelper.getUser()));
        bundle.putString("pageName", "SwitchIdentity");
        intent.putExtra("info", bundle);
        activity.startActivity(intent);
    }

    @Override
    public void handle(String action, ReadableMap params) {
        super.handle(action, params);

        if (TextUtils.equals(action, "switchIdentity")) {
            if (AccountHelper.getType() == 1) {
                AccountHelper.setType(2);
            } else {
                AccountHelper.setType(1);
            }
            restartApp();
        }
    }

    private void restartApp() {
        Intent i = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
}
